package com.hujf.netty.heartBeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.time.LocalDate;

/**
 * @author Hujf
 * @title: ServerHandler
 * @date 2021/2/3 0003上午 9:32
 * @description: TODO
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {


    /**
     *  心跳转发器
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent){
            //向下强转
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventype = null;
            switch (event.state()){
                case READER_IDLE:
                    eventype="读空闲";
                    break;
                case WRITER_IDLE:
                    eventype="写空闲";
                    break;
                case ALL_IDLE:
                    eventype="读写空闲";
                    break;
                default:
            }
            System.out.println(ctx.channel().remoteAddress()+"空闲"+eventype);
            ctx.close();
        }
    }
}
